/*
 * Copyright (c) 2010, 2012 Luan Nguyen Thanh. All rights reserved.
 * 
 * This code is written by Luan Nguyen Thanh, with any partners involved.
 * The unauthorized use of this code should not be permitted!
 */
package MyIM.client.view.form;

import MyIM.client.callback.CallBackRemote;
import MyIM.client.engine.Engine;
import MyIM.client.view.ClientView;
import MyIM.client.view.component.HintTextField;
import MyIM.client.view.component.JTKButton;
import MyIM.client.view.component.JTextButton;
import MyIM.client.view.lnf.MetroStyle;
import MyIM.client.view.lnf.SegoeFont;
import MyIM.utilities.Status;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author Luan Nguyen Thanh <tklarryonline@gmail.com>
 */
public class Dialog_Login extends JFrame implements ActionListener {

    private HintTextField txtUsername;
    private JPasswordField txtPassword;
    private JButton btnLogin, btnRegister;
    private JTextButton btnForgotPwd;
    private Engine engine;

    public Dialog_Login(Engine engine) {
        this.engine = engine;
        init();
        initListener();
    }

    public Dialog_Login() {
        init();
    }

    private void init() {
        // Initializes the frame
        setTitle("MyIM - Login");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setIconImage(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE));

        // Initializes the components        
        txtUsername = new HintTextField("Your username", 23);
        txtPassword = new JPasswordField();

        btnLogin = new JButton("Login");
        btnLogin.setActionCommand("login");
        btnLogin.setFont(MetroStyle.BUTTON_FONT);
        btnLogin.setForeground(Color.white);
        btnLogin.setPressedIcon(new ImageIcon(new BufferedImage(1,
                1, BufferedImage.TYPE_INT_ARGB_PRE)));
        btnLogin.setVerticalTextPosition(JButton.CENTER);
        btnLogin.setHorizontalTextPosition(JButton.CENTER);
        getRootPane().setDefaultButton(btnLogin);

        btnForgotPwd = new JTextButton("Forgot your password?", SegoeFont.getSegoeUIFont("normal", 15.0f));
        btnForgotPwd.setActionCommand("forgot_password");
        btnForgotPwd.setHorizontalAlignment(SwingConstants.TRAILING);

        btnRegister = new JTKButton("Sign-up? :)");
        btnRegister.setActionCommand("register");
        btnRegister.setFont(MetroStyle.BUTTON_FONT);
        btnRegister.setPressedIcon(new ImageIcon(new BufferedImage(1,
                1, BufferedImage.TYPE_INT_ARGB_PRE)));
        btnRegister.setVerticalTextPosition(JButton.CENTER);
        btnRegister.setHorizontalTextPosition(JButton.CENTER);

        initLayout();

        setResizable(false);
        pack();
        setLocationRelativeTo(null);
    }

    private void initLayout() {
        // Sets layout
        setLayout(new GridBagLayout());
        GridBagConstraints cons = new GridBagConstraints();
        cons.anchor = GridBagConstraints.CENTER;
        cons.fill = GridBagConstraints.VERTICAL;

        cons.gridx = 0;
        cons.gridy = 0;
        cons.insets = new Insets(5, 18, 18, 18);
        add(makeLoginPanel(), cons);
    }

    private JPanel makeLoginPanel() {
        // Prepares components
        Font title = MetroStyle.FORM_TITLE;
        Font header = MetroStyle.FORM_HEADER;
        Insets headerInsets = new Insets(5, 3, 2, 5);
        Insets textFieldInsets = new Insets(4, 5, 4, 5);
        JLabel label;

        // Prepares layout
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints cons = new GridBagConstraints();
        cons.anchor = GridBagConstraints.FIRST_LINE_START;
        cons.fill = GridBagConstraints.BOTH;

        // Starts adding components
        cons.gridx = 0;
        cons.gridy = 0;
        cons.gridwidth = 2;
        cons.insets = new Insets(3, 1, 10, 5);
        label = new JLabel("Login");
        label.setFont(title);
        label.setForeground(MetroStyle.LIGHTER_MARINE);
        panel.add(label, cons);

        cons.gridy++;
        cons.insets = headerInsets;
        label = new JLabel("Username");
        label.setFont(header);
        panel.add(label, cons);

        cons.gridy++;
        cons.insets = textFieldInsets;
        panel.add(txtUsername, cons);

        cons.gridy++;
        cons.insets = headerInsets;
        label = new JLabel("Password");
        label.setFont(header);
        panel.add(label, cons);

        cons.gridy++;
        cons.insets = textFieldInsets;
        panel.add(txtPassword, cons);

        cons.gridy++;
        cons.insets = new Insets(1, 5, 5, 5);
        panel.add(btnForgotPwd, cons);

        cons.gridy++;
        cons.insets = new Insets(7, 4, 4, 6);
        cons.gridwidth = 1;
        cons.weightx = 0.8;
        panel.add(btnLogin, cons);

        cons.gridx++;
        cons.insets = new Insets(7, 6, 4, 4);
        cons.weightx = 0.2;
        panel.add(btnRegister, cons);

        return panel;
    }

    private void initListener() {
        txtUsername.addActionListener(this);
        txtUsername.setActionCommand("txtUsername");
        txtUsername.requestFocus();

        txtPassword.addActionListener(this);
        txtPassword.setActionCommand("login");

        btnLogin.addActionListener(this);
        btnRegister.addActionListener(this);
        btnForgotPwd.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (cmd.equalsIgnoreCase("login")) {
            String username = txtUsername.getText();
            String password = txtPassword.getText();
            try {

                Status status = engine.login(username, password);
                System.out.println(status);
                if (status == Status.SUCCESS) {
                    engine.getClient().setCientView(new ClientView(engine));


                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(this,
                            "Wrong username or password");
                }
            } catch (RemoteException ex) {
                Logger.getLogger(Dialog_Login.class.getName()).log(Level.SEVERE,
                        null, ex);
            }
        } else if (cmd.equalsIgnoreCase("register")) {
            this.dispose();
            new Dialog_Register(engine);
        } else if (cmd.equalsIgnoreCase("forgot_password")) {
            JOptionPane.showMessageDialog(this, "Really?");
        } else if (cmd.equalsIgnoreCase("txtUsername")) {
            txtPassword.requestFocus();
        }
    }
}